package lc.com.architecture.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import lc.com.architecture.i.IPresenter;

/**
 * @author LiuChao
 * @describe
 * @date 2017/11/3
 * @contact email:450127106@qq.com
 */

public abstract class BaseActivity<P extends IPresenter> extends RxAppCompatActivity {

    public BaseApplication mApplication;
    public Context mContext;
    private Unbinder mUnbinder;
    @Inject
    P mIPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mUnbinder = ButterKnife.bind(this);
        mApplication = (BaseApplication) getApplication();
        mContext = this;
        initView();
        componentInject();
        initData();
    }

    public abstract void initView();

    public abstract void initData();

    public abstract int getLayoutId();

    /**
     * 依赖注入的入口
     */
    protected abstract void componentInject();

    @Override
    protected void onStart() {
        if (mIPresenter != null) {
            mIPresenter.onStart();
        }
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        if (mIPresenter != null) {
            mIPresenter.onDestory();
        }
        if (mUnbinder != null && mUnbinder != Unbinder.EMPTY)
            mUnbinder.unbind();
        super.onDestroy();
    }
}
